﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;

namespace Fcdbas.Platform.Properties
{
    public class DataItemConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(String)) return true;

            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(String)) return true;

            if (destinationType == typeof(InstanceDescriptor)) return true;

            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            String result = "";
            if (destinationType == typeof(String))
            {
                DataItem item = (DataItem)value;
                result = item.ColumnName + "," +
                    item.DataType.ToString() + "," +
                    item.Length.ToString() + "," +
                    item.LogicalParent + "," +
                item.Simple.ToString() + "," + 
                item.UseEditor.ToString() + "," +
                item.Queryable.ToString() + "," +
                item.OnlyInWhereClause + "," +
                item.Validate + "," +
                item.Reference + "," +
                item.EnumMethod + "," +
                item.Caption + "," +
                item.FormatOption  + "," + 
                item.Required.ToString() + "," +
                item.Insertable.ToString() + "," +
                item.Flags.ToString() + "," +
                item.UpdateOption.ToString();
                return result;

            }

            if (destinationType == typeof(InstanceDescriptor))
            {
                ConstructorInfo ci = typeof(DataItem).GetConstructor(new Type[] { typeof(string), typeof(Type),typeof(int),typeof(string),
                    typeof(string),typeof(bool),typeof(bool),typeof(string),typeof(string),typeof(string),typeof(string),
                    typeof(string),typeof(string), typeof(bool),typeof(bool),typeof(Enums.DataItemFlags),typeof(Enums.DataItemUpdate)});
                DataItem di = (DataItem)value;
                return new InstanceDescriptor(ci, new object[] { di.ColumnName ,
                    di.DataType,
                    di.Length ,
                    di.LogicalParent ,
                di.Simple ,
                di.UseEditor,
                di.Queryable,
                di.OnlyInWhereClause ,
                di.Validate,
                di.Reference,
                di.EnumMethod,
                di.Caption,
                di.FormatOption ,
                di.Required,
                di.Insertable,
                di.Flags,
                di.UpdateOption});
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string)
            {
                String[] v = ((String)value).Split(',');
                if (v.GetLength(0) != 2)
                {
                    throw new ArgumentException("Invalid parameter format");
                }

                DataItem di = new DataItem();
                di.ColumnName =v[0];
                    di.DataType=Type.GetType(v[1]);
                    di.Length=int.Parse(v[2]);
                    di.LogicalParent =v[3];
                di.Simple = bool.Parse(v[4]);
                di.UseEditor = bool.Parse(v[5]);
                di.Queryable = bool.Parse(v[6]);
                di.OnlyInWhereClause =bool.Parse(v[7]);
                di.Validate= v[8];
                di.Reference=v[9];
                di.EnumMethod=v[10];
                di.Caption=v[11];
                di.FormatOption =v[12];
                di.Required=bool.Parse(v[13]);
                di.Insertable=bool.Parse(v[14]);
                di.Flags = (Enums.DataItemFlags)Enum.Parse(typeof(Enums.DataItemFlags),v[15]);
                di.UpdateOption = (Enums.DataItemUpdate)Enum.Parse(typeof(Enums.DataItemUpdate), v[16]);
                return di;
            }
            return base.ConvertFrom(context, culture, value);
        }
    }
}
